//
//  SliderView.swift
//  StoreProject
//
//  Created by CYC on 1998/8/12.
//

import UIKit

class SliderView1: UIView {
    
    typealias SliderView1Block = (_ value: CGFloat) -> Void
    var sureBlock: SliderView1Block?
    
    var value = CGFloat(0.5)
    var processView = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.creatUIAction()
    }
    
    required init?(coder: NSCoder) {
        fatalError("")
    }
    
    
    // MARK:创建UI
    @objc func creatUIAction() {
        
        let pointSize = CGFloat(12)
        let lineSize = CGFloat(3)
        let spaceSize = (self.c_width() - pointSize*5)/4.0
        
        if true {
            
            let view1 = UIView.init(frame: self.bounds)
            view1.backgroundColor = .clear
            self.addSubview(view1)
            
            let line = UIView.init(frame: CGRect.init(x: 0, y: (view1.c_height() - lineSize)/2.0, width: view1.c_width(), height: lineSize))
            line.backgroundColor = kContentColor
            line.c_radius(radius: line.c_height()/2.0)
            view1.addSubview(line)
            
            for index in 0..<5 {
                
                let point = UIView.init(frame: CGRect.init(x: (pointSize + spaceSize)*CGFloat(index), y: (view1.c_height() - pointSize)/2.0, width: pointSize, height: pointSize))
                point.backgroundColor = kContentColor
                point.c_radius(radius:pointSize/2.0)
                view1.addSubview(point)
            }
        }
        
        if true {
            
            let view1 = UIView.init(frame: self.bounds)
            view1.backgroundColor = .clear
            view1.clipsToBounds = true
            self.addSubview(view1)
            self.processView = view1
            
            let line = UIView.init(frame: CGRect.init(x: 0, y: (view1.c_height() - lineSize)/2.0, width: view1.c_width(), height: lineSize))
            line.backgroundColor = kMainColor
            line.c_radius(radius: line.c_height()/2.0)
            view1.addSubview(line)
            
            for index in 0..<5 {
                
                let point = UIView.init(frame: CGRect.init(x: (pointSize + spaceSize)*CGFloat(index), y: (view1.c_height() - pointSize)/2.0, width: pointSize, height: pointSize))
                point.backgroundColor = kMainColor
                point.c_borderAndRadius(width: 2, color: .white, radius: point.c_height()/2.0)
                view1.addSubview(point)
            }
        }
        
        self.updateUIAction()
    }
    
    // MARK:更新UI
    @objc func updateUIAction() {
        
        
        self.processView.frame = .init(x: self.processView.c_left(), y: self.processView.c_top(), width: self.c_width()*self.value, height: self.processView.c_height())
        
        if self.sureBlock != nil {
            self.sureBlock!(self.value)
        }
    }
    
    // MARK:计算value
    @objc func caculationAction(point: CGPoint) {
        
        if point.x < 0 {
            self.value = 0
        } else if point.x > self.c_width() {
            self.value = 1
        } else {
        
            // 正常处理
            self.value = CGFloat(point.x / self.c_width())
        }
        self.updateUIAction()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let touch = touches.first
        let point = touch?.location(in: self)
        self.caculationAction(point: point!)
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let touch = touches.first
        let point = touch?.location(in: self)
        self.caculationAction(point: point!)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        print(self.value)
    }
    
    

}


class SliderView2: UIView {
    
    typealias SliderView2Block = (_ value: CGFloat) -> Void
    var sureBlock: SliderView2Block?
    
    var value = CGFloat(0.5)
    var processView = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.creatUIAction()
    }
    
    required init?(coder: NSCoder) {
        fatalError("")
    }
    
    
    // MARK:创建UI
    @objc func creatUIAction() {
        
        let lineSize = CGFloat(3)
        
        if true {
            
            let view1 = UIView.init(frame: self.bounds)
            view1.backgroundColor = .clear
            self.addSubview(view1)
            
            let line = UIView.init(frame: CGRect.init(x: 0, y: (view1.c_height() - lineSize)/2.0, width: view1.c_width(), height: lineSize))
            line.backgroundColor = kContentColor
            line.c_radius(radius: line.c_height()/2.0)
            view1.addSubview(line)
        }
        
        if true {
            
            let view1 = UIView.init(frame: .zero)
            view1.backgroundColor = kMainColor
            view1.clipsToBounds = true
            view1.c_borderAndRadius(width: 2, color: .white, radius: 2)
            self.addSubview(view1)
            self.processView = view1
            
        }
        
        self.updateUIAction()
    }
    
    // MARK:更新UI
    @objc func updateUIAction() {
        
        var left = self.c_width() * self.value
        if left > self.c_width() - 10 {
            left = self.c_width() - 10
        }
        
        self.processView.frame = .init(x: left, y: (self.c_height() - 15)/2.0, width: 10, height: 15)
        
        
        if self.sureBlock != nil {
            self.sureBlock!(self.value)
        }
    }
    
    // MARK:计算value
    @objc func caculationAction(point: CGPoint) {
        
        if point.x < 0 {
            self.value = 0
        } else if point.x > self.c_width() {
            self.value = 1
        } else {
        
            // 正常处理
            self.value = CGFloat(point.x / self.c_width())
        }
        self.updateUIAction()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let touch = touches.first
        let point = touch?.location(in: self)
        self.caculationAction(point: point!)
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let touch = touches.first
        let point = touch?.location(in: self)
        self.caculationAction(point: point!)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        print(self.value)
    }
    
    

}
